load("@bazel_skylib//rules:common_settings.bzl", "string_flag")
load("//tachyon/math/elliptic_curves/short_weierstrass/generator:build_defs.bzl", "generate_ec_points")
load(
    "//tachyon/math/finite_fields/generator/prime_field_generator:build_defs.bzl",
    "SMALL_SUBGROUP_ADICITY",
    "SMALL_SUBGROUP_BASE",
    "SUBGROUP_GENERATOR",
    "generate_large_fft_prime_fields",
)

package(default_visibility = ["//visibility:public"])

string_flag(
    name = "fq_" + SMALL_SUBGROUP_ADICITY,
    build_setting_default = "1",
)

string_flag(
    name = "fq_" + SMALL_SUBGROUP_BASE,
    build_setting_default = "3",
)

string_flag(
    name = "fq_" + SUBGROUP_GENERATOR,
    build_setting_default = "3",
)

# Parameters are from https://www.secg.org/sec2-v2.pdf#page=13 and https://github.com/arkworks-rs/curves/tree/master/secp256k1/src/fields
generate_large_fft_prime_fields(
    name = "fq",
    class_name = "Fq",
    # Hex: 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f
    modulus = "115792089237316195423570985008687907853269984665640564039457584007908834671663",
    namespace = "tachyon::math::secp256k1",
    small_subgroup_adicity = ":fq_" + SMALL_SUBGROUP_ADICITY,
    small_subgroup_base = ":fq_" + SMALL_SUBGROUP_BASE,
    subgroup_generator = ":fq_" + SUBGROUP_GENERATOR,
)

string_flag(
    name = "fr_" + SMALL_SUBGROUP_ADICITY,
    build_setting_default = "1",
)

string_flag(
    name = "fr_" + SMALL_SUBGROUP_BASE,
    build_setting_default = "3",
)

string_flag(
    name = "fr_" + SUBGROUP_GENERATOR,
    build_setting_default = "7",
)

generate_large_fft_prime_fields(
    name = "fr",
    class_name = "Fr",
    # Hex: 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141
    modulus = "115792089237316195423570985008687907852837564279074904382605163141518161494337",
    namespace = "tachyon::math::secp256k1",
    small_subgroup_adicity = ":fr_" + SMALL_SUBGROUP_ADICITY,
    small_subgroup_base = ":fr_" + SMALL_SUBGROUP_BASE,
    subgroup_generator = ":fr_" + SUBGROUP_GENERATOR,
)

generate_ec_points(
    name = "curve",
    a = ["0"],
    b = ["7"],
    base_field = "Fq",
    base_field_degree = 1,
    base_field_dep = ":fq",
    base_field_hdr = "tachyon/math/elliptic_curves/secp/secp256k1/fq.h",
    gen_gpu = True,
    namespace = "tachyon::math::secp256k1",
    scalar_field = "Fr",
    scalar_field_dep = ":fr",
    scalar_field_hdr = "tachyon/math/elliptic_curves/secp/secp256k1/fr.h",
    # Hex: 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798
    x = ["55066263022277343669578718895168534326250603453777594175500187360389116729240"],
    # Hex: 0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8
    y = ["32670510020758816978083085130507043184471273380659243275938904335757337482424"],
)
